#! /usr/bin/python
# -*- coding:utf8 -*- 

import Queue
import math

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from mainGraphics import MainView

class MainWidget(QWidget):
    def __init__(self, parent=None):
        super(MainWidget, self).__init__(parent)
        self.__initObjects()
        self.__setObjects()
        self.__setObjectsStyle()
        self.__setLayoutRelation()
        self.setWindowIcon(QIcon(QPixmap('./1.png')))

        self.get_initial_points_btn.clicked.connect(
                self.get_initial_points)
        self.get_end_points_btn.clicked.connect(
                self.get_end_points)
        self.run_btn.clicked.connect(
                self.run)
        self.restart_btn.clicked.connect(self.restart)
        self.restart_btn.clicked.connect(
                self.view.scene.restart)


    def __initObjects(self):
        self.get_initial_points_btn = QPushButton(u"获取初始点")
        self.get_end_points_btn = QPushButton(u"获取终止点")
        self.run_btn = QPushButton(u"运动")
        self.restart_btn = QPushButton(u"再来一次")

        self.view = MainView()

        self.btn_width = 80
        self.btn_height = 40
        self.left_layout = QVBoxLayout()
        self.right_layout = QHBoxLayout()
        self.layout = QHBoxLayout()

    def __setObjects(self):
        self.get_initial_points_btn.setFixedSize(self.btn_width,
                self.btn_height)
        self.get_end_points_btn.setFixedSize(self.btn_width, self.btn_height)
        self.run_btn.setFixedSize(self.btn_width, self.btn_height)
        self.restart_btn.setFixedSize(self.btn_width, self.btn_height)
        self.get_initial_points_btn.setCheckable(True)
        self.get_end_points_btn.setCheckable(True)
        self.left_layout.setSpacing(1)
        self.right_layout.setSpacing(1)
        self.left_layout.setContentsMargins(0, 0, 0, 0)
        self.right_layout.setContentsMargins(0, 0, 0, 0)
        self.layout.setSpacing(0)
        self.layout.setContentsMargins(0, 0, 0, 0)

    def __setObjectsStyle(self):
        start = '''
            QPushButton{
                background-color: #003366;
                border-radius: 0px;
                color: white;
                }
            QPushButton:Hover{
                color: #CC9999;
                }
            QPushButton:pressed{
                background-color: #CC3333;
                }
            QPushButton:checked{
                background-color: #111111;
                }
        '''
        info = '''
            QLabel{
                background-color: #CC9999;
                border-radius: 0px;
                color: black;
                }
        '''
        self.get_initial_points_btn.setStyleSheet(start)
        self.get_end_points_btn.setStyleSheet(start)
        self.run_btn.setStyleSheet(start)
        self.restart_btn.setStyleSheet(start)

    def __setLayoutRelation(self):
        self.left_layout.addWidget(self.get_initial_points_btn)
        self.left_layout.addWidget(self.get_end_points_btn)
        self.left_layout.addWidget(self.run_btn)
        self.left_layout.addWidget(self.restart_btn)
        self.left_layout.addStretch(1)
        self.right_layout.addWidget(self.view)
        self.layout.addLayout(self.left_layout)
        self.layout.addLayout(self.right_layout)
        self.setLayout(self.layout)

    def get_initial_points(self):
        self.get_end_points_btn.setChecked(False)
        self.view.scene.get_initial_points()

    def get_end_points(self):
        self.get_initial_points_btn.setChecked(False)
        self.view.scene.get_end_points()

    def run(self):
        self.get_initial_points_btn.setChecked(False)
        self.get_end_points_btn.setChecked(False)
        self.view.scene.run()
    
    def restart(self):
        self.get_initial_points_btn.setChecked(False)
        self.get_end_points_btn.setChecked(False)
        self.view.scene.restart()

if __name__ == "__main__":
    import os
    import sys
    app = QApplication(sys.argv)
    mw = MainWidget()
    mw.show()
    sys.exit(app.exec_())
